package com.jsoft.android.as;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class RF extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		/*
		 * Make sure we have something we can use
		 * http://developer.android.com/guide/topics/data/data-storage.html
		 */
		String state = Environment.getExternalStorageState();
		Log.d("external storage state", state);

		// TODO scope these
		boolean mExternalStorageAvailable;
		boolean mExternalStorageWriteable;
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			// Something else is wrong. It may be one of many other states, but
			// all we need
			// to know is we can neither read nor write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}

		Log.d("bundle extra", "?????");
		Bundle extras = getIntent().getExtras();
		// TODO did not see if anything is there
		String fn = getIntent().getData().toString();
		Log.d("file Uri we got", fn);
		
		// DataUri
		//Uri intent = getIntent().getData();
		

		// and If you're using API Level 8 or greater, use getExternalFilesDir()
		String s = "\"";
		//String s = "\"";
		
		List<String> list = new ArrayList();
		
		if (mExternalStorageAvailable) {
			try {
				File sdCard = Environment.getExternalStorageDirectory();
				// File dir = new File (sdCard.getAbsolutePath() + "/test");
				// dir.mkdirs();
				File f = new File(fn);
				BufferedReader in = new BufferedReader(new FileReader(f));

				String buf = "";
				
				while ((buf = in.readLine()) != null) {
					Log.d("read my file - s line", buf);
					// convert all the " to "" in buf
					//s = s + '"' + ',' + '"' + buf;
					list.add(buf);
				}
				//s = s + '"';
				//s = in.readLine();
				//Log.d("read my file - as a string", s);

				in.close();

			} catch (Exception e) { // what exceptions should we look for
				Log.d("OutputFile", e.getMessage());
			}
		}
		
        Bundle bundle = new Bundle();

        // loop over the list
        // put into a string buffer
        // each line ends in a ',' (except the last one)
        // each " in the line is replaced with ""
        s = "\""+list.get(0)+"\"";
        //s = list.get(0);
        //for (int i=1; i<15; i++) {
        for (int i=1; i<list.size(); i++) {
        	// make sure we get any "
        	String myS = list.get(i).replace("\"", "\"\"");
        	//s = s + "\n" + "\"" + myS + "\"";
        	s = s + "," + "\"" + myS + "\"";
        }
		Log.d("returning this string", s);

        //String sTest = "\"line 1\",\"line \"\"2\"\"\",\"line 3\"";
        //bundle.putString("APP_INVENTOR_RESULT", sTest);
    	//bundle.putCharSequence("APP_INVENTOR_RESULT", list.toString());
    	bundle.putCharSequence("APP_INVENTOR_RESULT", s);
    	//bundle.putCharSequence("APP_INVENTOR_RESULT", "\"line 1\",\"line \"\"2\"\"\",\"line 3\"");
   	    Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        
        finish();
	}
}
